<script setup>

import {computed} from "vue";

const props = defineProps({
  total: {
    required: true,
    type: Number
  },
  page: {
    type: Number,
    default: 1
  },
  limit: {
    type: Number,
    default: 10
  },
  pageSizes: {
    type: Array,
    default: () => [10, 20, 30, 40, 50]
  },
  layout: {
    type: String,
    default: '->, total, sizes, prev, pager, next, jumper'
  }
})

const emit = defineEmits(['update:page','update:limit','pagination']);

const currentPage = computed({
  get() {
    return props.page
  },
  set(val) {
    emit('update:page', val)
  }
})

const pageSize = computed({
  get() {
    return props.limit
  },
  set(val) {
    emit('update:limit', val)
  }
})

const handleSizeChange = (val) => {
  if (currentPage.value * val > props.total) {
    currentPage.value = 1
  }
  emit('pagination', {page: currentPage.value, limit: val})
}

const handleCurrentChange = (val) => {
  emit('pagination', {page: val, limit: pageSize.value})
}
</script>

<template>
  <a-pagination
      class="mt-4 pagination"
      v-model:current="currentPage"
      v-model:page-size="pageSize"
      :page-size-options="pageSizes"
      :total="total"
      @show-size-change="handleSizeChange"
      @change="handleCurrentChange"
      show-quick-jumper
      show-size-changer
      :show-total="total => `共 ${total} 条`"
  >
    <template #buildOptionText="props">
      <span v-if="props.value !== '50'">{{ props.value }}页</span>
      <span v-else>全部</span>
    </template>
  </a-pagination>
</template>

<style scoped lang="scss">
.pagination{
  display: flex;
  justify-content: flex-end;
  @apply pb-1rem;

  :deep(.ant-select){
    width: 70px;
  }
}
</style>
